package com.gxnu.edu.cn.client.ui;

import com.gxnu.edu.cn.client.clientConfig.Client;
import com.gxnu.edu.cn.client.controller.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

@Component
public class LoginFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JTextField ipField;
    @Autowired
    private UserClient userClient;

    @Autowired
    private Client client;

    @Autowired
    private MainFrame mainFrame;

    public LoginFrame() {
        // 空构造函数
    }

    private void initComponents() {
        setTitle("即时通讯软件 - 登录");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 添加标题
        JLabel titleLabel = new JLabel("即时通讯软件");
        titleLabel.setFont(new Font("SimHei", Font.BOLD, 24));
        titleLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
        mainPanel.add(titleLabel);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));

        // 添加用户名和密码输入框
        JPanel inputPanel = new JPanel(new GridLayout(2, 2, 10, 10));
        inputPanel.add(new JLabel("服务器地址:"));
        ipField = new JTextField();
        inputPanel.add(ipField);
        inputPanel.add(new JLabel("用户名:"));
        usernameField = new JTextField();
        inputPanel.add(usernameField);
        inputPanel.add(new JLabel("密码:"));
        passwordField = new JPasswordField();
        inputPanel.add(passwordField);
        mainPanel.add(inputPanel);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));

        // 添加按钮面板
        JPanel buttonPanel = new JPanel();
        JButton loginButton = new JButton("登录");
        JButton registerButton = new JButton("注册");

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                String ip = ipField.getText();
                if (username.isEmpty() || password.isEmpty() || ip.isEmpty()) {
                    JOptionPane.showMessageDialog(
                            LoginFrame.this,
                            "服务器地址、用户名和密码不能为空",
                            "登录提示",
                            JOptionPane.WARNING_MESSAGE
                    );
                    return;
                }

                loginButton.setEnabled(false);
                registerButton.setEnabled(false);

                Map<String, String> credentials = new HashMap<>();
                credentials.put("username", username);
                credentials.put("password", password);

                // 不进行连接操作，直接登录
                userClient.login(credentials, ip).whenComplete((result, ex) -> {
                    SwingUtilities.invokeLater(() -> {
                        loginButton.setEnabled(true);
                        registerButton.setEnabled(true);

                        if (ex != null) {
                            JOptionPane.showMessageDialog(
                                    LoginFrame.this,
                                    "登录失败: " + ex.getMessage(),
                                    "错误",
                                    JOptionPane.ERROR_MESSAGE
                            );
                            return;
                        }

                        if (result != null) {
                            dispose();
                            mainFrame.init(username, ip); // 传递 IP 地址
                            mainFrame.setVisible(true);
                        } else {
                            JOptionPane.showMessageDialog(
                                    LoginFrame.this,
                                    result != null ? result : "登录失败，未知错误",
                                    "登录提示",
                                    JOptionPane.ERROR_MESSAGE
                            );
                        }
                    });
                });
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new RegisterFrame(userClient).display();
            }
        });

        buttonPanel.add(loginButton);
        buttonPanel.add(registerButton);
        mainPanel.add(buttonPanel);

        add(mainPanel);
    }

    // 显示登录窗口的方法
    public void display() {
        SwingUtilities.invokeLater(() -> {
            initComponents();
            setVisible(true);
        });
    }
}